﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlTinhThanh : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (ViewState["flag"] == null)
            {
                ViewState["flag"] = false;
            }

        }
    }
    void ShowMessageBox(string text)
    {
        MessageBox.Text = text;
        MessageBox.ShowOnPageLoad = true;
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtName.Text = "";
        txtMaSo.Text = "New";
        ViewState["flag"] = true;
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        int i=0;
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                i = dt.ExecuteSQl_int(str, "k_insertTinhThanh", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@Name", txtName.Text));
                if (i == 1)
                {
                    ShowMessageBox("Thêm mới tỉnh/thành thành công");
                }
                else
                {
                    ShowMessageBox("Hệ thống đang bảo trì");
                }
                ViewState["flag"] = false;
                txtMaSo.Text = "";
                txtName.Text = "";
            }
            else
            {
                //update
                i = dt.ExecuteSQl_int(str, "k_updateTinhThanh", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@MaTinh",txtMaSo.Text),
                    new SqlParameter("@Name", txtName.Text));
                if (i == 1)
                {
                    ShowMessageBox("Cập nhật tỉnh/thành thành công");
                }
                else
                {
                    ShowMessageBox("Đề nghị cung cấp mã tỉnh/thành để cập nhật");
                }
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }    
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }
    void LoadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "k_searchTinhThanh", CommandType.StoredProcedure,
                new SqlParameter("@Cont", txtSearch.Text));
            grvTinhThanh.DataSource = db;
            grvTinhThanh.DataBind();
            if (db.Rows.Count == 0)
            {
                ShowMessageBox("Không tìm thấy kết quả");
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }
    protected void grvTinhThanh_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        if (e.CommandName == "chon")
        {

            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            Label lblMaSo = (Label)grv.FindControl("lblMaSo");
            Label lblTinhThanh = (Label)grv.FindControl("lblTinhThanh");
            txtMaSo.Text = lblMaSo.Text;
            txtName.Text = lblTinhThanh.Text;

            grvTinhThanh.DataSource = null;
            grvTinhThanh.DataBind();

            ViewState["flag"] = false;
            TabContainer1.ActiveTabIndex = 0;

        }
    }
    protected void grvTinhThanh_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvTinhThanh.PageIndex = e.NewPageIndex;
        LoadGrid();
    }
}